{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximum Matching of Players With Trainers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array #two-pointers #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组 #双指针 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: matchPlayersAndTrainers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #运动员和训练师的最大匹配数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>players</code>&nbsp;，其中&nbsp;<code>players[i]</code>&nbsp;表示第 <code>i</code>&nbsp;名运动员的 <strong>能力</strong>&nbsp;值，同时给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>trainers</code>&nbsp;，其中&nbsp;<code>trainers[j]</code>&nbsp;表示第 <code>j</code>&nbsp;名训练师的 <strong>训练能力值</strong>&nbsp;。</p>\n",
    "\n",
    "<p>如果第 <code>i</code>&nbsp;名运动员的能力值 <strong>小于等于</strong>&nbsp;第 <code>j</code>&nbsp;名训练师的能力值，那么第&nbsp;<code>i</code>&nbsp;名运动员可以 <strong>匹配</strong>&nbsp;第&nbsp;<code>j</code>&nbsp;名训练师。除此以外，每名运动员至多可以匹配一位训练师，每位训练师最多可以匹配一位运动员。</p>\n",
    "\n",
    "<p>请你返回满足上述要求&nbsp;<code>players</code>&nbsp;和 <code>trainers</code>&nbsp;的 <strong>最大</strong> 匹配数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>players = [4,7,9], trainers = [8,2,5,8]\n",
    "<b>输出：</b>2\n",
    "<b>解释：</b>\n",
    "得到两个匹配的一种方案是：\n",
    "- players[0] 与 trainers[0] 匹配，因为 4 &lt;= 8 。\n",
    "- players[1] 与 trainers[3] 匹配，因为 7 &lt;= 8 。\n",
    "可以证明 2 是可以形成的最大匹配数。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>players = [1,1,1], trainers = [10]\n",
    "<b>输出：</b>1\n",
    "<b>解释：</b>\n",
    "训练师可以匹配所有 3 个运动员\n",
    "每个运动员至多只能匹配一个训练师，所以最大答案是 1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= players.length, trainers.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= players[i], trainers[j] &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximum-matching-of-players-with-trainers](https://leetcode.cn/problems/maximum-matching-of-players-with-trainers/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximum-matching-of-players-with-trainers](https://leetcode.cn/problems/maximum-matching-of-players-with-trainers/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[4,7,9]\\n[8,2,5,8]', '[1,1,1]\\n[10]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        # players = [4,7,9], trainers = [8,2,5,8]\n",
    "        heapify(players)\n",
    "        heapify(trainers) \n",
    "        res = 0\n",
    "        print(players, trainers)\n",
    "        while players:\n",
    "            p = heappop(players)\n",
    "            while trainers:\n",
    "                t = heappop(trainers)\n",
    "                if p <= t:\n",
    "                    res += 1\n",
    "                    break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        sorted_players = sorted(players)\n",
    "        sorted_trainers = sorted(trainers)\n",
    "        res = 0\n",
    "        i, j = 0, 0\n",
    "        while i < len(sorted_players) and j < len(sorted_trainers):\n",
    "            if sorted_players[i] <= sorted_trainers[j]:\n",
    "                res += 1\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif sorted_players[i] > sorted_trainers[j]:\n",
    "                j += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        最大匹配数 = 0\n",
    "        ni, nj, i, j = len(players), len(trainers), 0, 0\n",
    "        while i < ni and j < nj:\n",
    "            if players[i] <= trainers[j]:\n",
    "                最大匹配数 += 1\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return 最大匹配数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        left_1 = 0\n",
    "        left_2 = 0\n",
    "        res = 0\n",
    "        while 1:\n",
    "            if players[left_1] <= trainers[left_2]:\n",
    "               res += 1\n",
    "               left_1 += 1\n",
    "               left_2 += 1\n",
    "            else:\n",
    "                left_2 += 1\n",
    "            if left_1 >= len(players) or left_2 >= len(trainers) :\n",
    "                break\n",
    "        return res\n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        players_index = 0\n",
    "        trainers_index = 0\n",
    "        cnt = 0\n",
    "        while players_index < len(players) and trainers_index < len(trainers):\n",
    "            if players[players_index] <= trainers[trainers_index]:\n",
    "                cnt += 1\n",
    "                players_index += 1\n",
    "                trainers_index += 1\n",
    "            else:\n",
    "                trainers_index += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        count = 0\n",
    "        index = 0\n",
    "        for player in players:\n",
    "            while index < len(trainers) and player > trainers[index]:\n",
    "                index = index + 1\n",
    "            if index < len(trainers):\n",
    "                count = count + 1\n",
    "                index = index + 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        i = j = 0\n",
    "        ans = 0\n",
    "        while i < len(players) and j < len(trainers):\n",
    "            if players[i] <= trainers[j]:\n",
    "                ans += 1\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        sum1 = 0\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        i = len(players)-1\n",
    "        j = len(trainers)-1\n",
    "        while i >= 0 and j >= 0:\n",
    "            if trainers[j] >= players[i]:\n",
    "                j -= 1\n",
    "                i -= 1\n",
    "                sum1 += 1\n",
    "            else:\n",
    "                i -= 1\n",
    "        return sum1\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        nt = len(trainers)\n",
    "        off = res = 0\n",
    "        for p in players:\n",
    "            off = bisect_left(trainers, p, off)\n",
    "            if off == nt: break\n",
    "            off += 1\n",
    "            res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        l = 0\n",
    "        cnt = 0\n",
    "        for x in players:\n",
    "            while l < len(trainers) and x > trainers[l]:\n",
    "                l += 1\n",
    "            if l == len(trainers):\n",
    "                return cnt\n",
    "            cnt += 1\n",
    "            l += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans = 0\n",
    "        n, m = len(players), len(trainers)\n",
    "        p = q = 0\n",
    "        while p < n and q < m:\n",
    "            if players[p] <= trainers[q]:\n",
    "                ans += 1\n",
    "                p += 1\n",
    "                q += 1\n",
    "            else:\n",
    "                q += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        m, n = len(players), len(trainers)\n",
    "        i = j = count = 0\n",
    "\n",
    "        while i < m and j < n:\n",
    "            while j < n and players[i] > trainers[j]:\n",
    "                j += 1\n",
    "            if j < n:\n",
    "                count += 1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        \n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "\n",
    "        match = 0\n",
    "        for i in range(len(trainers)):\n",
    "            if match < len(players) and players[match] <= trainers[i]:\n",
    "                match += 1\n",
    "        return match"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans = 0\n",
    "        j = 0\n",
    "        for i in players:\n",
    "            while 1:\n",
    "                if j == len(trainers):\n",
    "                    break\n",
    "                if trainers[j]<i:\n",
    "                    j += 1\n",
    "                else:\n",
    "                    break\n",
    "            if j<len(trainers) and trainers[j] >= i:\n",
    "                ans += 1\n",
    "            # print(j)\n",
    "            j += 1\n",
    "            if j == len(trainers):\n",
    "                break\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        j,m = 0,len(trainers)\n",
    "        \n",
    "        for i,p in enumerate(players):\n",
    "            while j<m and trainers[j]<p:\n",
    "                j+=1\n",
    "            if j==m:\n",
    "                return i\n",
    "            j += 1\n",
    "        return len(players)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        num=0\n",
    "        right=len(players)-1\n",
    "        right2=len(trainers)-1\n",
    "        while right2>=0 and right>=0:\n",
    "            if trainers[right2]>=players[right]:\n",
    "                num+=1\n",
    "                right-=1\n",
    "                right2-=1\n",
    "            else:\n",
    "                right-=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        p, t = 0, 0\n",
    "        while p != len(players) and t != len(trainers):\n",
    "            if players[p] <= trainers[t]:\n",
    "                p += 1\n",
    "                t += 1\n",
    "            elif(players[p] > trainers[t]):\n",
    "                t += 1\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans = curP = curT = 0\n",
    "        lT, lP = len(trainers), len(players)\n",
    "        while curP < lP and curT < lT:\n",
    "            if players[curP] <= trainers[curT]:\n",
    "                ans += 1\n",
    "                curP += 1\n",
    "                curT += 1\n",
    "            else:\n",
    "                curT += 1\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        np = len(players)\n",
    "        nt = len(trainers)\n",
    "        n = min(np,nt)\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans = 0\n",
    "        i,j = 0,0\n",
    "        while i<np and j<nt:\n",
    "            if trainers[j]>=players[i]:\n",
    "                ans += 1\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return ans\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        # 与糖果题一样，降序排序，然后用最大的训练师不断去匹配最大的运动员即可\n",
    "        res = 0\n",
    "        trainers.sort(reverse=True)\n",
    "        players.sort(reverse=True)\n",
    "        n,m = len(trainers),len(players)\n",
    "        i,j=0,0\n",
    "        while i<n and j<m:\n",
    "            if trainers[i]>=players[j]:\n",
    "                res+=1\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                j += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans = curP = curT = 0\n",
    "        lT, lP = len(trainers), len(players)\n",
    "        for t in trainers:\n",
    "            if players[curP] <= t:\n",
    "                ans += 1\n",
    "                curP += 1\n",
    "            if curP == lP:\n",
    "                break\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        i,j = 0,0\n",
    "        res = 0\n",
    "        while i < len(players) and j < len(trainers):\n",
    "            if players[i] <= trainers[j]:\n",
    "                res += 1\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                j += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        m, n = len(players), len(trainers)\n",
    "        l, r = 0, 0\n",
    "        res = 0\n",
    "        while l < m and r < n:\n",
    "            if players[l] <= trainers[r]:\n",
    "                l += 1\n",
    "                res += 1\n",
    "            r += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans = cur = 0\n",
    "        n = len(players)\n",
    "        for t in trainers:\n",
    "            if players[cur] <= t:\n",
    "                ans += 1\n",
    "                cur += 1\n",
    "            if cur == n:\n",
    "                break\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        i,j = 0,0\n",
    "        match_num = 0\n",
    "        while i <len(players) and j < len(trainers):\n",
    "            if players[i]<=trainers[j]:\n",
    "                match_num += 1\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                j += 1\n",
    "        return match_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        i = j = 0\n",
    "        players.sort(reverse = True)\n",
    "        trainers.sort(reverse = True)\n",
    "        while i<len(players) and j<len(trainers):\n",
    "            if players[i]<=trainers[j]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "                continue\n",
    "            i+=1\n",
    "        return j\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        n = len(players)\n",
    "        m = len(trainers)\n",
    "        j = 0\n",
    "        \n",
    "        for i in range(n):\n",
    "            while j < m and trainers[j] < players[i]:\n",
    "                j += 1\n",
    "            if j == m:\n",
    "                return i\n",
    "            j += 1\n",
    "        return i + 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        i = j = 0\n",
    "        players.sort(reverse = True)\n",
    "        trainers.sort(reverse = True)\n",
    "        while i<len(players) and j<len(trainers):\n",
    "            if players[i]<=trainers[j]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "                continue\n",
    "            i+=1\n",
    "        return j\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        m, n = len(players), len(trainers)\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        i = j = 0\n",
    "        ans = 0\n",
    "        while i < m and j < n:\n",
    "            if players[i] <= trainers[j]:\n",
    "                ans += 1\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        curP = curT = 0\n",
    "        lT = len(trainers)\n",
    "        lP = len(players)\n",
    "        ans = 0\n",
    "        while curP < lP and curT < lT:\n",
    "            if players[curP] <= trainers[curT]:\n",
    "                ans += 1\n",
    "                curP += 1\n",
    "                curT += 1\n",
    "            else:\n",
    "                curT += 1\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        最大匹配数 = 0\n",
    "        ni, nj, i, j = len(players), len(trainers), 0, 0\n",
    "        while i < ni and j < nj:\n",
    "            if players[i] <= trainers[j]:\n",
    "                最大匹配数 += 1\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return 最大匹配数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        res = 0\n",
    "        trainers.sort(reverse=True)\n",
    "        players.sort(reverse=True)\n",
    "        n,m = len(trainers),len(players)\n",
    "        i,j=0,0\n",
    "        while i<n and j<m:\n",
    "            if trainers[i]>=players[j]:\n",
    "                res+=1\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                j += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        最大匹配数 = 0\n",
    "        ni, nj, i, j = len(players), len(trainers), 0, 0\n",
    "        while i < ni and j < nj:\n",
    "            if players[i] <= trainers[j]:\n",
    "                最大匹配数 += 1\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return 最大匹配数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans=0\n",
    "        j=0\n",
    "        i=0\n",
    "        while i<len(players):\n",
    "            while j<len(trainers):\n",
    "                if players[i]<=trainers[j]:\n",
    "                    ans+=1\n",
    "                    j+=1\n",
    "                    break\n",
    "                else:\n",
    "                    j+=1\n",
    "            i+=1\n",
    "        return ans\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans = 0\n",
    "        n = len(players)\n",
    "        for t in trainers:\n",
    "            if players[ans] <= t:\n",
    "                ans += 1\n",
    "            if ans == n:\n",
    "                break\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        i, j = 0, 0\n",
    "        ans = 0\n",
    "        while i < len(players) and j < len(trainers):\n",
    "            if players[i] <= trainers[j]:\n",
    "                ans += 1\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        ans=i=j=0\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        pn=len(players)\n",
    "        tn=len(trainers)\n",
    "        while i<pn:\n",
    "            while j<tn:\n",
    "                if trainers[j]>=players[i]:\n",
    "                    j+=1\n",
    "                    ans+=1\n",
    "                    break\n",
    "                j+=1\n",
    "            i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: list[int], trainers: list[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        trainerIndex = 0\n",
    "        n = len(trainers)\n",
    "        result = 0\n",
    "        for player in players:\n",
    "            while trainerIndex<n:\n",
    "                if player <= trainers[trainerIndex]:\n",
    "                    result+=1\n",
    "                    trainerIndex+=1\n",
    "                    break\n",
    "                trainerIndex+=1\n",
    "            else:\n",
    "                break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        m, n = len(players), len(trainers)\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        i = j = 0\n",
    "        ans = 0\n",
    "        while i < m and j < n:\n",
    "            if players[i] <= trainers[j]:\n",
    "                ans += 1\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        ans = 0\n",
    "        i,j = 0,0\n",
    "        for i,x in enumerate(players):\n",
    "            while j<len(trainers) and trainers[j] < x: j += 1\n",
    "            if j == len(trainers): break\n",
    "            ans += 1\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "\n",
    "        i, j = 0, 0\n",
    "        count = 0\n",
    "        while i < len(players) and j < len(trainers):\n",
    "            if players[i] <= trainers[j]:\n",
    "                count += 1\n",
    "                i+=1\n",
    "                j+=1\n",
    "            else:\n",
    "                j+=1\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "\n",
    "        i = 0\n",
    "        j = 0\n",
    "\n",
    "        ans = 0\n",
    "        while i < len(players) and j < len(trainers):\n",
    "            if players[i] <= trainers[j]:\n",
    "                ans += 1\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                j += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        m, n = len(players), len(trainers)\n",
    "        i = j = count = 0\n",
    "\n",
    "        while i < m and j < n:\n",
    "            while j < n and players[i] > trainers[j]:\n",
    "                j += 1\n",
    "            if j < n:\n",
    "                count += 1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        \n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        left, right = 0, 0\n",
    "        m, n = len(players), len(trainers)\n",
    "        ans = 0   \n",
    "        while left <m and right <n:\n",
    "            if players[left] <= trainers[right]:\n",
    "                left +=1\n",
    "                ans +=1\n",
    "            right+=1\n",
    "\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        m, n = len(players), len(trainers)\n",
    "        i = j = count = 0\n",
    "\n",
    "        while i < m and j < n:\n",
    "            while j < n and players[i] > trainers[j]:\n",
    "                j += 1\n",
    "            if j < n:\n",
    "                count += 1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        \n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        n = len(players)\n",
    "        m = len(trainers)\n",
    "        i = 0\n",
    "        j = 0\n",
    "        while i < n and j < m:\n",
    "            if players[i] <= trainers[j]:\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return i\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        m, n = len(players), len(trainers)\n",
    "        i = j = count = 0\n",
    "\n",
    "        while i < m and j < n:\n",
    "            while j < n and players[i] > trainers[j]:\n",
    "                j += 1\n",
    "            if j < n:\n",
    "                count += 1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        i = j = 0\n",
    "        n = len(players)\n",
    "        m = len(trainers)\n",
    "        ans = 0\n",
    "        while i < n and j < m:\n",
    "            if trainers[j] >= players[i]:\n",
    "                i += 1\n",
    "                ans += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players = sorted(players)\n",
    "        trainers = sorted(trainers)\n",
    "        now = 0\n",
    "        ans = 0\n",
    "        for x in players:\n",
    "            while(now < len(trainers) and trainers[now] < x):\n",
    "                now = now + 1\n",
    "            if now == len(trainers):\n",
    "                break\n",
    "            else:\n",
    "                ans += 1\n",
    "                now += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        m = len(players)\n",
    "        n = len(trainers)\n",
    "        if m==1 or n==1:\n",
    "            if players[0] <= trainers[0]:\n",
    "                return 1\n",
    "            else: return 0\n",
    "        else:\n",
    "            players = list(sorted(players))\n",
    "            trainers = list(sorted(trainers))\n",
    "            i = 0\n",
    "            j = 0\n",
    "            \n",
    "            while i <m and j < n:\n",
    "                if players[i] <= trainers[j]:\n",
    "                    i+=1\n",
    "                    j+=1\n",
    "                else: j+=1\n",
    "\n",
    "\n",
    "\n",
    "            return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        players=sorted(players)\n",
    "        trainers=sorted(trainers)\n",
    "        m=len(players)\n",
    "        n=len(trainers)\n",
    "        i=0\n",
    "        j=0\n",
    "        ans=0\n",
    "        while i<m and j<n:\n",
    "            if players[i]<=trainers[j]:\n",
    "                ans+=1\n",
    "                i+=1\n",
    "                j+=1\n",
    "            else:\n",
    "                j+=1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:\n",
    "        pLen,tLen = len(players),len(trainers)\n",
    "        count = 0\n",
    "        pIndex = 0\n",
    "        tIndex = 0\n",
    "        players.sort()\n",
    "        trainers.sort()\n",
    "        while(pIndex<pLen and tIndex<tLen):\n",
    "            if(players[pIndex]<=trainers[tIndex]):\n",
    "                count += 1\n",
    "                pIndex += 1\n",
    "                tIndex += 1\n",
    "            else:\n",
    "                tIndex += 1\n",
    "        return count\n",
    "             \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
